package com.shopping.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.shopping.bean.Admin;
import com.shopping.bean.OrderGoods;
import com.shopping.bean.Review;
import com.shopping.bean.User;
import com.shopping.dao.AdminDao;
import com.shopping.util.DBUtil;

public class AdminDaoImpl implements AdminDao {
	
	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	@Override
	public List<User> selectAllUser(Map<String,String[]> condition) {
		String sql = "select * from users where 1=1 ";
		int pageNum = 1;
		int pageSize = 5;
		
		List list = new ArrayList();
		for(String key:condition.keySet()) {
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "or userName LIKE ? ";
				list.add("%"+condition.get(key)[0]+"%");
			}
		}
		try {
			pageNum = Integer.parseInt(condition.get("pageNum")[0]);
		}catch(Exception e) {
			
		}
		try {
			pageSize = Integer.parseInt(condition.get("pageSize")[0]);
		}catch(Exception e) {
			
		}
		sql += "LIMIT ?,?";
		list.add((pageNum-1)*pageSize);
		list.add(pageSize);
		System.out.println("dao" + pageNum +":" +pageSize);
		List<User> users = jt.query(sql, new BeanPropertyRowMapper<User>(User.class),list.toArray());
		return users;
	}
	@Override
	public Admin selectAdmin(String adminName, String password) {
		String sql = "select * from admin where adminName = ? and password = ?";
		List<Admin> list = jt.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),adminName,password);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}
	
	
	@Override
	public int deleteUserById(int userId) {
		String sql = "delete from users where userId = ?";
		return jt.update(sql,userId);
	}
	@Override
	public int updateUser(User newUser, User oldUser) {
		String sql = "update users set userName = ?,password = ?,userSex = ?,address = ?,phone = ?,userImage = ? where userName = ? and password = ?";
		return jt.update(sql,newUser.getUserName(),newUser.getPassWord(),newUser.getUserSex(),newUser.getAddress(),newUser.getPhone(),newUser.getUserImage(),oldUser.getUserName(),oldUser.getPassWord());
	}
	@Override
	public int deleteReviewById(int reviewId) {
		String sql = "delete from Review where rId = ?";
		return jt.update(sql,reviewId);
	}
	@Override
	public int updateReview(Review newReview, Review oldReview) {
		String sql = "update Review set goodsId = ?,rContent = ?,userId = ?,rTime = ? where rId = ?";
		return jt.update(sql,newReview.getGoodsId(),newReview.getrContent(),newReview.getUserId(),newReview.getrTime(),oldReview.getrId());
	}
	@Override
	public int deleteOrderGoodsById(int oId) {
		String sql = "delete from orderGoods where oId = ?";
		return jt.update(sql,oId);
	}
	@Override
	public int updateOrderGoods(OrderGoods newOrderGoods, OrderGoods oldOrderGoods) {
		String sql = "update OrderGoods set priceSum = ?,userId = ? where oId = ?";
		return jt.update(sql,newOrderGoods.getPriceSum(),newOrderGoods.getUserId(),oldOrderGoods.getOid());
	}
	@Override
	public Admin selectAdminByName(String adminName) {
		String sql = "select * from admin where adminName = ?";
		List<Admin> list = jt.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class),adminName);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}
	@Override
	public int selectUserTotalRows(Map<String, String[]> condition) {
		String sql ="SELECT count(1) FROM users WHERE 1 = 1 ";
		List params = new ArrayList(); //由于sql语句是动态的 ?的个数 不确定，所以没拼接一个sql 我们就先把这个参数放在 list集合当中
		for(String key: condition.keySet()) {
			if("keyword".equals(key) && !"".equals(condition.get(key)[0])) {
				sql += "and userName like ? ";
				params.add(condition.get(key)[0]);
			}
			
		}
		
			return jt.queryForObject(sql, Integer.class,params.toArray());
	}

}
